package com.cardbaobao.cardbabyclient.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.api.Api;
import com.cardbaobao.cardbabyclient.buried.Google;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.buried.SelfUtils;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.callback.ItemClickedCallback;
import com.cardbaobao.cardbabyclient.controller.CommonJumpController;
import com.cardbaobao.cardbabyclient.model.Banner;
import com.cardbaobao.cardbabyclient.model.BusinessClass;
import com.cardbaobao.cardbabyclient.model.Event;
import com.cardbaobao.cardbabyclient.model.HeadCommon;
import com.cardbaobao.cardbabyclient.model.QrCodeDebitFeeRateData;
import com.cardbaobao.cardbabyclient.model.QuickDebitFeeRateData;
import com.cardbaobao.cardbabyclient.model.RealName;
import com.cardbaobao.cardbabyclient.model.dao.LauncherAdClickedDao;
import com.cardbaobao.cardbabyclient.model.result.BannerResult;
import com.cardbaobao.cardbabyclient.model.result.HasBindCardResult;
import com.cardbaobao.cardbabyclient.model.result.HeadCommonResult;
import com.cardbaobao.cardbabyclient.model.result.QrCodeDebitFeeRateResult;
import com.cardbaobao.cardbabyclient.model.result.QuickDebitFeeRateResult;
import com.cardbaobao.cardbabyclient.ui.activity.AccountLoginActivity;
import com.cardbaobao.cardbabyclient.ui.activity.AddCreditCardActivity;
import com.cardbaobao.cardbabyclient.ui.activity.AddCreditCardPreActivity;
import com.cardbaobao.cardbabyclient.ui.activity.CardDoProgressActivity;
import com.cardbaobao.cardbabyclient.ui.activity.CreditRepaymentActivity;
import com.cardbaobao.cardbabyclient.ui.activity.CreditToMoneyActivity;
import com.cardbaobao.cardbabyclient.ui.activity.DebitIntroActivity;
import com.cardbaobao.cardbabyclient.ui.activity.FeeRateActivity;
import com.cardbaobao.cardbabyclient.ui.activity.IdentifyVerifyActivity;
import com.cardbaobao.cardbabyclient.ui.activity.QrCodeDebitActivity;
import com.cardbaobao.cardbabyclient.ui.activity.QuickDebitActivity;
import com.cardbaobao.cardbabyclient.ui.activity.SpeedCardActivity;
import com.cardbaobao.cardbabyclient.ui.activity.SpeedLoanActivity;
import com.cardbaobao.cardbabyclient.ui.activity.UpperQuotaActivity;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.ui.adapter.BannerListAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.base.CommonAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.base.ViewHolder;
import com.cardbaobao.cardbabyclient.ui.fragment.base.BaseFragment;
import com.cardbaobao.cardbabyclient.ui.pop.LauncherAdPop;
import com.cardbaobao.cardbabyclient.utils.ApkUtil;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.buried.GoogleAnalyticsUtil;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.JsonUtil;
import com.cardbaobao.cardbabyclient.utils.ScreenUtil;
import com.cardbaobao.cardbabyclient.utils.SharedPreferencesUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;
import com.cardbaobao.cardbabyclient.widget.MyGridView;
import com.cardbaobao.cardbabyclient.widget.RotateHeaderHandler;
import com.stx.xhb.xbanner.XBanner;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrUIHandler;
import rx.Observable;
import rx.Subscriber;


/**
 * Description:
 * Created by FHW
 * on 2017/6/9
 */

public class WalletFragment extends BaseFragment {
    private static final int APP_BAR_SCROLL_UP=1;//上滑
    private static final int APP_BAR_SCROLL_DOWN=2;//下滑
    private static final int APP_BAR_SCROLL_EXPAND=3;//已经展开
    private String prePageId;
    public void setBuriedInfo(String prePageId){
        this.prePageId=prePageId;
    }
    private static final int TYPE_QUICK_DEBIT=1;
    private static final int TYPE_QR_CODE_DEBIT=2;
    @BindView(R.id.ptr_view)
    PtrFrameLayout ptr_view;

    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    private int appBarVerticalOffset;
    private int appBarScrollState=APP_BAR_SCROLL_EXPAND;

    @BindView(R.id.fl_banner_container)
    FrameLayout flBannerContainer;

    @BindView(R.id.xb_banner)
    XBanner xBanner;

    @BindView(R.id.gv_menu)
    MyGridView gvMenu;

    @BindView(R.id.gv_business)
    MyGridView gvBusiness;

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private Class<?> toActivity = null;

    private List<Banner.CarouselFigureListBean> topBannerList = new ArrayList<>();
    private List<BusinessClass.TabListBean> walletMenuList = new ArrayList<>();

    private List<BusinessClass.TabListBean> businessClassList = new ArrayList<>();

    private List<Banner.CarouselFigureListBean> bottomBannerList = new ArrayList<>();


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_wallet;
    }

    @Override
    protected void initViewFinish(View view, Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        mCurrentPageId=Self.Page.app_wallet_homepage;
        //轮播图宽高比例750:340
        final int screenWidth = ScreenUtil.getScreenWidth();
        ViewGroup.LayoutParams params = flBannerContainer.getLayoutParams();
        params.width = screenWidth;
        params.height = (int) ((screenWidth * 34) / 75.0f);
        flBannerContainer.setLayoutParams(params);

        businessClassList = new ArrayList<>();

        initIntent();

        final PtrUIHandler header = new RotateHeaderHandler(mContext);
        ptr_view.setHeaderView((View) header);
        ptr_view.addPtrUIHandler(header);
        ptr_view.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                //下拉刷新
                getHeadCommonData();
            }
        });
        ptr_view.disableWhenHorizontalMove(true);
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (verticalOffset == 0) {
                    ptr_view.setEnabled(true);
                } else {
                    ptr_view.setEnabled(false);
                }
                if(verticalOffset<appBarVerticalOffset){
                    //越来越小 -3<-2 向上滑
                    appBarScrollState=APP_BAR_SCROLL_UP;
                }else if(verticalOffset>appBarVerticalOffset){
                    //越来越大 -2>-3 向下滑
                    appBarScrollState=APP_BAR_SCROLL_DOWN;
                }else{
                    appBarScrollState=APP_BAR_SCROLL_EXPAND;
                }
                appBarVerticalOffset =verticalOffset;
            }
        });

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                ptr_view.autoRefresh();
            }
        }, 200);
        //计算间隔时间
        long intervalTime = System.currentTimeMillis() -
                SharedPreferencesUtil.CbbInfo.getLatestCloseAdPopTime();
        //避免同时显示更新提示框与广告弹窗
        if (intervalTime > Constant.MILLISECOND_24_HOUR) {
            //获取广告
            getLauncherAd();
        }else{
            //版本更新
            startCheckUpdate();
        }
    }

    private void getLauncherAd() {
        final List<String> clickedUrls = new LauncherAdClickedDao().getAllClickedAdUrl();
        Observable<String> observable = Api.getBannerData(Constant.GLB0327);
        observable.subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String s) {
                BannerResult bannerResult = JsonUtil.json2Object(s, BannerResult.class);
                if (bannerResult != null) {
                    if (bannerResult.getCode() == Constant.SUC_CODE) {
                        if (bannerResult.getData() != null) {
                            List<Banner.CarouselFigureListBean> source =
                                    bannerResult.getData().getCarousel_figure_list();
                            //去除已经点击过的广告
                            if (source != null && source.size() > 0) {
                                List<Banner.CarouselFigureListBean> data = new ArrayList<>();
                                String url;
                                for (Banner.CarouselFigureListBean bean : source) {
                                    url = bean.getUrl();
                                    if (!TextUtils.isEmpty(url) && !clickedUrls.contains(bean.getUrl())) {
                                        data.add(bean);
                                    }
                                }
                                if (data.size() > 0) {
                                    final LauncherAdPop pop = new LauncherAdPop(mContext, data);
                                    pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
                                        @Override
                                        public void onDismiss() {
                                            pop.stopXBannerLoop();
                                            //避免同时显示更新提示框与广告弹窗
                                            //关掉弹窗广告后，开始检查版本更新
                                            startCheckUpdate();
                                        }
                                    });
                                    View view = getView();
                                    if (view != null) {
                                        pop.showAtLocation(getView(), Gravity.CENTER, 0, 0);
                                    }
                                }else{
                                    //避免同时显示更新提示框与广告弹窗
                                    startCheckUpdate();
                                }
                            }else{
                                //避免同时显示更新提示框与广告弹窗
                                startCheckUpdate();
                            }
                        }
                    }
                }
            }
        });
    }

    private void startCheckUpdate(){
        ApkUtil.updateVersion(mContext, false, null);
    }

    /**
     * 头部数据
     */
    private void getHeadCommonData() {
        Observable<String> observable = Api.getCommonHeader(Constant.ZCC0108);
        observable.subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {
                ptr_view.refreshComplete();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.showToast(ApplicationHelper.
                        getApplicationContext().
                        getString(R.string.string_request_server_fail));
                ptr_view.refreshComplete();
            }

            @Override
            public void onNext(String s) {
                HeadCommonResult headCommonResult = JsonUtil.json2Object(s, HeadCommonResult.class);
                if (headCommonResult.getCode() == Constant.SUC_CODE) {
                    if (headCommonResult.getData() != null) {
                        HeadCommon common = headCommonResult.getData();
                        if (common != null) {
                            //快捷收款、二维码收款等
                            List<BusinessClass.TabListBean> tempWalletList =
                                    common.getWallet_tab_list();
                            //顶部轮播
                            List<Banner.CarouselFigureListBean> tempTopBanner =
                                    common.getCarousel_figure_list();
                            if (tempWalletList != null && tempWalletList.size() > 0) {
                                walletMenuList.clear();
                                //业务过滤
                                BusinessClass.TabListBean bean;
                                for (int i = 0; i < tempWalletList.size(); i++) {
                                    bean = tempWalletList.get(i);
                                    if (bean.getState() == Constant.BUSINESS_STATUS_NORMAL) {
                                        walletMenuList.add(bean);
                                    }
                                }
                            }
                            if (tempTopBanner != null && tempTopBanner.size() > 0) {
                                xBanner.stopAutoPlay();
                                topBannerList.clear();
                                topBannerList.addAll(tempTopBanner);
                            }
                            //急速办卡、急速贷款等
                            List<BusinessClass.TabListBean> tempBusinessList =
                                    common.getTab_list();
                            if (tempBusinessList != null && tempBusinessList.size() > 0) {
                                businessClassList.clear();
                                BusinessClass.TabListBean bean;
                                for (int i = 0; i < tempBusinessList.size(); i++) {
                                    bean = tempBusinessList.get(i);
                                    if (bean.getState() == Constant.BUSINESS_STATUS_NORMAL) {
                                        businessClassList.add(bean);
                                    }
                                }
                            }
                            //底部轮播
                            List<Banner.CarouselFigureListBean> tempBottomBanner =
                                    common.getCarousel_figure_list_small();
                            if (tempBottomBanner != null && tempBottomBanner.size() > 0) {
                                bottomBannerList.clear();
                                bottomBannerList.addAll(tempBottomBanner);
                            }
                            setWalletMenuUi();
                            setBusinessUi();
                            setBottomBannerUi();
                        }
                    }
                } else {
                    ToastUtil.showToast(headCommonResult.getMsg());
                }
            }
        });
    }

    //获取快捷收款 收款费率
    private void getQuickDebitFeeRates() {
        Api.getQuickDebitFeeRate().subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable throwable) {
                dismissProgressDialog();
                ToastUtil.showToast(ApplicationHelper.getApplicationContext().
                        getString(R.string.string_request_data_fail));
            }

            @Override
            public void onNext(String s) {
                dismissProgressDialog();
                QuickDebitFeeRateResult result = JsonUtil.json2Object(s, QuickDebitFeeRateResult.class);
                if (result.getCode() == Constant.SUC_CODE) {
                    QuickDebitFeeRateData data = result.getData();
                    if (data != null) {
                        ArrayList<QuickDebitFeeRateData.FeeRate> rates = data.getRate_list();
                        if (rates != null && rates.size() > 0) {
                            boolean canQuickDebit = false;
                            for (QuickDebitFeeRateData.FeeRate r : rates) {
                                if (Constant.KQB010101.equals(r.getChannel_type())) {
                                    canQuickDebit = true;
                                    break;
                                }
                            }
                            if (canQuickDebit) {
                                Intent intent = new Intent();
                                intent.putParcelableArrayListExtra(FeeRateActivity.QUICK_DEBIT_FEE_RATE_LIST_ARRAY,
                                        rates);
                                intent.putExtra(FeeRateActivity.FEE_TYPE_I,
                                        FeeRateActivity.QUICK_DEBIT_FEE_TYPE);
                                intent.putExtra(BaseActivity.PRE_PAGE_ID_S,
                                        mCurrentPageId);
                                intent.setClass(mContext, QuickDebitActivity.class);
                                startActivity(intent);
                            } else {
                                ToastUtil.showToast(ApplicationHelper.getApplicationContext().
                                        getString(R.string.string_data_exception));
                            }
                        }
                    }
                } else {
                    ToastUtil.showToast(result.getMsg());
                }
            }
        });
    }

    //获取二维码收款 收款费率
    private void getQrCodeDebitFeeRates() {
        Api.getQrCodeDebitFeeRate().subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable throwable) {
                dismissProgressDialog();
                ToastUtil.showToast(ApplicationHelper.getApplicationContext().
                        getString(R.string.string_request_data_fail));
            }

            @Override
            public void onNext(String s) {
                dismissProgressDialog();
                QrCodeDebitFeeRateResult result =
                        JsonUtil.json2Object(s, QrCodeDebitFeeRateResult.class);
                if (result.getCode() == Constant.SUC_CODE) {
                    QrCodeDebitFeeRateData data = result.getData();
                    if (data != null) {
                        ArrayList<QrCodeDebitFeeRateData.FeeRate> rates = data.getRateList();
                        if (rates != null && rates.size() > 0) {
                            Intent intent = new Intent();
                            intent.setClass(mContext, QrCodeDebitActivity.class);
                            intent.putExtra(FeeRateActivity.FEE_TYPE_I,
                                    FeeRateActivity.QR_CODE_DEBIT_FEE_TYPE);
                            intent.putExtra(FeeRateActivity.QR_CODE_DEBIT_FEE_RATE_LIST_ARRAY,
                                    rates);
                            intent.putExtra(BaseActivity.PRE_PAGE_ID_S,mCurrentPageId);
                            startActivity(intent);
                        } else {
                            ToastUtil.showToast(ApplicationHelper.getApplicationContext().
                                    getString(R.string.string_data_exception));
                        }
                    }
                } else {
                    ToastUtil.showToast(result.getMsg());
                }
            }
        });
    }

    private void isUserBindDebitCard(final int type) {
        showProgressDialog(true);
        Observable<String> observable =
                Api.checkBindCard(String.valueOf(
                        mApplication.getUser().getID()));
        observable.subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable throwable) {
                dismissProgressDialog();
                ToastUtil.showToast(
                        ApplicationHelper.getApplicationContext().getString(
                                R.string.string_request_server_fail));
            }

            @Override
            public void onNext(String s) {
                HasBindCardResult result = JsonUtil.json2Object(s, HasBindCardResult.class);
                if(result.getCode()==0){
                    //未实名认证
                    dismissProgressDialog();
                    if(result.getMsg().contains("未实名认证")){
                        //未实名认证
                        Intent intent = new Intent(mContext, DebitIntroActivity.class);
                        intent.putExtra(DebitIntroActivity.DEBIT_TYPE_I,
                                type==TYPE_QUICK_DEBIT ?
                                        DebitIntroActivity.QUICK_DEBIT :
                                        DebitIntroActivity.QR_CODE_DEBIT);
                        intent.putExtra(BaseActivity.PRE_PAGE_ID_S,
                                mCurrentPageId);
                        startActivity(intent);
                    }else{
                        ToastUtil.showToast(result.getMsg());
                    }
                }else if(result.getCode()==1){
                    //已实名、已绑储蓄卡、已绑信用卡
                    if (type==TYPE_QUICK_DEBIT) {
                        //快捷收款
                        getQuickDebitFeeRates();
                    } else if (type==TYPE_QR_CODE_DEBIT) {
                        //二维码收款
                        getQrCodeDebitFeeRates();
                    }
                }else if(result.getCode()==2){
                    //已实名，未绑储蓄卡
                    dismissProgressDialog();
                    //未绑卡
                    RealName realName=result.getData();
                    Intent intent = new Intent(mContext, DebitIntroActivity.class);
                    intent.putExtra(DebitIntroActivity.DEBIT_TYPE_I,
                            type==TYPE_QUICK_DEBIT?
                                    DebitIntroActivity.QUICK_DEBIT :
                                    DebitIntroActivity.QR_CODE_DEBIT);
                    intent.putExtra(IdentifyVerifyActivity.NAME_S,
                            realName.getTrue_name());
                    intent.putExtra(IdentifyVerifyActivity.ID_CARD_NO_S,
                            realName.getId_card_no());
                    intent.putExtra(BaseActivity.PRE_PAGE_ID_S,
                            mCurrentPageId);
                    startActivity(intent);
                }else if(result.getCode()==3){
                    //已实名，未绑信用卡
                    if(type==TYPE_QR_CODE_DEBIT){
                        //二维码收款不需要绑定信用卡
                        getQrCodeDebitFeeRates();
                    }else{
                        //快捷收款需要绑定信用卡
                        dismissProgressDialog();
                        RealName realName=result.getData();
                        Intent intent=new Intent(mContext, AddCreditCardPreActivity.class);
                        intent.putExtra(AddCreditCardActivity.NAME_S,realName.getTrue_name());
                        intent.putExtra(AddCreditCardActivity.ID_CARD_NO_S,realName.getId_card_no());
                        intent.putExtra(BaseActivity.PRE_PAGE_ID_S,mCurrentPageId);
                        startActivity(intent);
                    }
                }
            }
        });
    }

    //快捷收款、二维码收款、还款、提额、顶部轮播
    private void setWalletMenuUi() {
        if (walletMenuList.size() > 0) {
            gvMenu.setVisibility(View.VISIBLE);
            xBanner.setVisibility(View.GONE);
            if (walletMenuList.size() < 4) {
                gvMenu.setNumColumns(walletMenuList.size());
            }else{
                gvMenu.setNumColumns(4);
            }
            gvMenu.setAdapter(new CommonAdapter<BusinessClass.TabListBean>(
                    mContext, walletMenuList, R.layout.item_wallet_menu) {
                @Override
                public void convert(ViewHolder holder, BusinessClass.TabListBean item, int position) {
                    holder.setText(R.id.tv_text, item.getName());
                    ImageLoaderUtil.displayImage(
                            URLUtil.CBB_IMG_URL + item.getPicture_path(),
                            (ImageView) holder.getView(R.id.iv_pic));
                }
            });
            gvMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    BusinessClass.TabListBean item = walletMenuList.get(position);
                    switch (item.getCode()) {
                        case Constant.JBQ01060501:
                            //快捷收款
                            handleQuickDebit();
                            SelfUtils.buriedClickData(Self.Action.app_click_wallet_homepage_quick_receivables,
                                    Self.Action.app_click_wallet_homepage_quick_receivables_des,
                                    mEnterPageTime,
                                    mCurrentPageId,
                                    prePageId);
                            break;
                        case Constant.JBQ01060502:
                            //二维码收款
                            handleQrCodeDebit();
                            SelfUtils.buriedClickData(Self.Action.app_click_wallet_homepage_qrcode_receivables,
                                    Self.Action.app_click_wallet_homepage_qrcode_receivables_des,
                                    mEnterPageTime,
                                    mCurrentPageId,
                                    prePageId);
                            break;
                        case Constant.JBQ01060503:
                            //还款
                            handleRepayment(1);
                            break;
                        case Constant.JBQ01060504:
                            //提额
                            handleUpQuota();
                            SelfUtils.buriedClickData(Self.Action.app_click_wallet_homepage_mention_the_amount_of,
                                    Self.Action.app_click_wallet_homepage_mention_the_amount_of_des,
                                    mEnterPageTime,
                                    mCurrentPageId,
                                    prePageId);
                            break;
                    }
                }
            });
        } else {
            gvMenu.setVisibility(View.GONE);
            xBanner.setVisibility(View.VISIBLE);
            xBanner.setData(topBannerList, null);
            xBanner.setmAdapter(new XBanner.XBannerAdapter() {
                @Override
                public void loadBanner(XBanner xBanner, Object o, View view, int i) {
                    ImageLoaderUtil.displayImage(
                            URLUtil.CBB_IMG_URL + ((Banner.CarouselFigureListBean) o).getImage(),
                            (ImageView) view);
                }
            });
            xBanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
                @Override
                public void onItemClick(XBanner xBanner, int i) {
                    CommonJumpController controller = new CommonJumpController(mContext);
                    Map<String, Object> extra = new HashMap<>();
                    extra.put(CommonJumpController.FROM_PAGE, "钱包-顶部banner");
                    controller.bannerJump(topBannerList.get(i), extra);
                }
            });
            xBanner.startAutoPlay();
        }
    }

    //急速办卡、急速贷款、办卡进度、信用变钱
    private void setBusinessUi() {
        gvBusiness.setAdapter(new CommonAdapter<BusinessClass.TabListBean>(
                mContext, businessClassList, R.layout.item_business_vertical) {
            @Override
            public void convert(ViewHolder holder, BusinessClass.TabListBean item, int position) {
                holder.setText(R.id.tv_name, item.getName());
                ImageLoaderUtil.displayImage(URLUtil.CBB_IMG_URL + item.getPicture_path(),
                        (ImageView) holder.getView(R.id.iv_icon));
            }
        });
        gvBusiness.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final BusinessClass.TabListBean item = businessClassList.get(position);
                mIntent.putExtra(BaseActivity.PRE_PAGE_ID_S,
                        mCurrentPageId);
                switch (item.getCode()) {
                    case Constant.JBQ01060101://极速办卡
                        SelfUtils.buriedClickData(Self.Action.app_click_index_speed_credit_card,
                                Self.Action.app_click_index_speed_credit_card_des,
                                mEnterPageTime,
                                mCurrentPageId,
                                prePageId);
                        mIntent.setClass(getActivity(), SpeedCardActivity.class);
                        mContext.startActivity(mIntent);
                        break;
                    case Constant.JBQ01060102://极速贷款
                        SelfUtils.buriedClickData(Self.Action.app_click_index_speed_loan,
                                Self.Action.app_click_index_speed_loan_des,
                                mEnterPageTime,
                                mCurrentPageId,
                                prePageId);
                        mIntent.setClass(getActivity(), SpeedLoanActivity.class);
                        mIntent.putExtra(SpeedLoanActivity.CODE, "");
                        mContext.startActivity(mIntent);
                        break;
                    case Constant.JBQ01060103://办卡进度
                        SelfUtils.buriedClickData(Self.Action.app_click_index_card_progress,
                                Self.Action.app_click_index_card_progress_des,
                                mEnterPageTime,
                                mCurrentPageId,
                                prePageId);
                        mIntent.setClass(mContext, CardDoProgressActivity.class);
                        toActivity = CardDoProgressActivity.class;
                        startActivity(mIntent);
                        GoogleAnalyticsUtil.setScreenAction(Google.CAREGORY_ORD,
                                "首页-" + item.getName());
                        break;
                    case Constant.JBQ01060104://信用评估
                        SelfUtils.buriedClickData(Self.Action.app_click_index_credit_evaluation,
                                Self.Action.app_click_index_credit_evaluation_des,
                                mEnterPageTime,
                                mCurrentPageId,
                                prePageId);
                        if (!mApplication.getIsLogin()) {
                            mIntent.setClass(mContext, AccountLoginActivity.class);
                            mIntent.putExtra(AccountLoginActivity.JUM_TYPE_I, 0);
                            mIntent.putExtra(AccountLoginActivity.LOGIN_REQUEST_FROM_S,
                                    Constant.PLY010221);
                            startActivity(mIntent);
                            getActivity().overridePendingTransition(R.anim.activity_in_bottom, R.anim.activity_out_top);
                        } else {
                            toActivity = CreditToMoneyActivity.class;
                            mIntent.setClass(mContext, CreditToMoneyActivity.class);
                            startActivity(mIntent);
                        }
                        GoogleAnalyticsUtil.setScreenAction(Google.CAREGORY_ORD,
                                "首页-" + item.getName());
                        break;
                }
            }
        });
    }

    //快捷收款
    private void handleQuickDebit(){
        if (mApplication.getIsLogin()) {
            isUserBindDebitCard(TYPE_QUICK_DEBIT);
        } else {
            Intent login = new Intent(mContext, AccountLoginActivity.class);
            login.putExtra(AccountLoginActivity.LOGIN_REQUEST_FROM_S,
                    Constant.PLY010219);
            startActivity(login);
        }
    }

    //二维码收款
    private void handleQrCodeDebit(){
        if (mApplication.getIsLogin()) {
            isUserBindDebitCard(TYPE_QR_CODE_DEBIT);
        } else {
            Intent login = new Intent(mContext, AccountLoginActivity.class);
            login.putExtra(AccountLoginActivity.LOGIN_REQUEST_FROM_S,
                    Constant.PLY010218);
            startActivity(login);
        }
    }

    //还款
    private void handleRepayment(int position){
        if (mApplication.getIsLogin()) {
            if(position==1){
                //顶部还款点击
                SelfUtils.buriedClickData(Self.Action.app_click_wallet_homepage_repayment,
                        Self.Action.app_click_wallet_homepage_repayment_des,
                        mEnterPageTime,
                        mCurrentPageId,
                        prePageId);
            }else if(position==2){
                //列表还款点击
                SelfUtils.buriedClickData(Self.Action.app_click_wallet_homepage_banner_repayment,
                        Self.Action.app_click_wallet_homepage_banner_repayment_des,
                        mEnterPageTime,
                        mCurrentPageId,
                        prePageId);
            }
            Intent repayment = new Intent(mContext, CreditRepaymentActivity.class);
            repayment.putExtra(BaseActivity.PRE_PAGE_ID_S,mCurrentPageId);
            startActivity(repayment);
        }else {
            Intent login = new Intent(mContext, AccountLoginActivity.class);
            login.putExtra(AccountLoginActivity.LOGIN_REQUEST_FROM_S,
                    Constant.PLY010220);
            startActivity(login);
        }
    }

    //提额
    private void handleUpQuota(){
        Intent upQuota = new Intent(mContext, UpperQuotaActivity.class);
        upQuota.putExtra(BaseActivity.PRE_PAGE_ID_S,mCurrentPageId);
        startActivity(upQuota);
    }

    //底部banner列表
    private void setBottomBannerUi() {
        BannerListAdapter adapter = new BannerListAdapter(bottomBannerList);
        adapter.setItemClickedListener(new ItemClickedCallback<Banner.CarouselFigureListBean>() {
            @Override
            public void onItemClicked(Banner.CarouselFigureListBean bean) {
                switch (bean.getPage_type()){
                    case 1:
                    case 2:
                        CommonJumpController controller = new CommonJumpController(mContext);
                        Map<String, Object> extra = new HashMap<>();
                        extra.put(CommonJumpController.FROM_PAGE, "钱包-底部banner");
                        if(mApplication.getIsLogin()){
                            controller.h5WebJump(bean.getPage_type(),
                                    bean.getUrl()+mApplication.getUser().getID(),
                                    bean.getDetail_name(),
                                    extra);
                        }else{
                            controller.h5WebJump(bean.getPage_type(),
                                    bean.getUrl(),
                                    bean.getDetail_name(),
                                    extra);
                        }
                        break;
                    case 3:
                        //快捷收款
                        handleQuickDebit();
                        break;
                    case 4:
                        //二维码收款
                        handleQrCodeDebit();
                        break;
                    case 5:
                        //还款
                        handleRepayment(2);
                        break;
                    case 6:
                        //提额
                        handleUpQuota();
                        break;
                }
            }
        });
        final LinearLayoutManager layoutManager =
                new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE&&
                        layoutManager.findFirstCompletelyVisibleItemPosition()==0) {
                            if(appBarScrollState==APP_BAR_SCROLL_DOWN||
                                    appBarScrollState==APP_BAR_SCROLL_EXPAND){
                                appBarLayout.setExpanded(true, true);
                            }
                    }
            }
        });
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(xBanner!=null){
            if (isVisible()) {
                xBanner.startAutoPlay();
            } else {
                xBanner.stopAutoPlay();
            }
        }
        if (isVisible()) {
            GoogleAnalyticsUtil.setScreenName(Google.NEW_INDEX);
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        SelfUtils.buriedBrowseData(mEnterPageTime,mCurrentPageId,prePageId);
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Event.LoginSucNotify loginSucNotify) {
        if (loginSucNotify.getType() == 0 && toActivity != null) {
            mIntent.setClass(mContext, toActivity);
            startActivity(mIntent);
            toActivity = null;
        }
    }

}
